__version__ = "1.1.0"
from playhouse.pool import PooledMySQLDatabase
from playhouse.shortcuts import ReconnectMixin
from ruamel.yaml import safe_load


DATABASE_HOST = '192.168.0.40'
DATABASE_PORT = 3306
DATABASE_NAME = 'cs_1'
DATABASE_USER = 'doumo'
DATABASE_PASS = 'doumo123'

def demo_config():
    """
    加载全局配置文件
    :return:
    """
    with open("../config/mysql.yaml", "r") as smallfile:
        config = safe_load(smallfile)  # pprint(config)
    return config


# cfg = demo_config()
# DATABASE_HOST = cfg['mysql']['ip']
# DATABASE_PORT = cfg['mysql']['port']
# DATABASE_NAME = 'mysql'
# DATABASE_USER = 'root'
# DATABASE_PASS = cfg['mysql']['rootPwd']

# 一次性连接
# once_db = MySQLDatabase(host=DATABASE_HOST, port=DATABASE_PORT, database=DATABASE_NAME, user=DATABASE_USER,password=DATABASE_PASS, charset='utf8mb4')


# 连接池
class ReconnectMySQLDatabase(ReconnectMixin, PooledMySQLDatabase):
    _instance = None

    @staticmethod
    def get_db_instance():
        if not ReconnectMySQLDatabase._instance:
            ReconnectMySQLDatabase._instance = ReconnectMySQLDatabase(host=DATABASE_HOST, port=DATABASE_PORT,
                                                                      database=DATABASE_NAME, user=DATABASE_USER,
                                                                      password=DATABASE_PASS, max_connections=32,
                                                                      stale_timeout=300, charset='utf8mb4')

        return ReconnectMySQLDatabase._instance


db_instance = ReconnectMySQLDatabase.get_db_instance()
